package com.chenyaowei.web.controller.product;

import cn.hutool.core.collection.CollectionUtil;
import com.chenyaowei.common.annotation.Anonymous;
import com.chenyaowei.common.annotation.RepeatSubmit;
import com.chenyaowei.common.constant.HttpStatus;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.core.page.TableDataInfo;
import com.chenyaowei.product.domain.CywProSkuInfo;
import com.chenyaowei.product.request.ProductReleaseRequest;
import com.chenyaowei.product.request.ProductSearchRequest;
import com.chenyaowei.product.service.CywProductService;
import com.chenyaowei.product.vo.ProductInfoVO;
import com.chenyaowei.product.vo.ProductSearchConditionVO;
import com.chenyaowei.product.vo.ProductStatsInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品发布
 *
 * @author 陈耀威
 * @date 2023-11-24
 */
@RestController
@RequestMapping("/product")
public class CywProductController extends BaseController {

    @Autowired
    private CywProductService productService;

    @Anonymous
    @GetMapping("/recommend")
    public AjaxResult productRecommend(){
        List<CywProSkuInfo> list = productService.productRecommend();
        return success(list);
    }

    @RepeatSubmit(interval = 3000)
    @PostMapping("/collect/{spuId}")
    private AjaxResult collectSpu(@PathVariable("spuId") Long spuId){
        return toAjax(productService.collectSpu(spuId));
    }

    /**
     * 商品统计信息：库存、销量...
     * @return
     */
    @Anonymous
    @GetMapping("/statsInfo/{skuId}")
    public AjaxResult productStatsInfo(@PathVariable Long skuId){
        ProductStatsInfoVO productStatsInfo  = productService.productStatsInfo(skuId);
        return success(productStatsInfo);
    }


    /**
     * 商品详情
     * @return
     */
    @Anonymous
    @GetMapping("/info/{spuId}")
    public AjaxResult productInfo(@PathVariable Long spuId, @RequestHeader(value = "Authorization", required = false) String token){
        ProductInfoVO productInfoVO  = productService.productInfo(spuId, token);
        return success(productInfoVO);
    }


    /**
     * 商品检索
     * 别问为什么用 post，问就是懒
     * @return
     */
    @Anonymous
    @PostMapping("/search")
    public TableDataInfo productSearch(@Validated @RequestBody ProductSearchRequest productSearchRequest){
        List<CywProSkuInfo> list  = productService.productSearch(productSearchRequest);
        if (CollectionUtil.isEmpty(list)){
            return null;
        }
        // TODO 逻辑分页（数据量大时内存溢出，不推荐） -> 上传es后，通过es的分页方式实现
        Integer pageNum = productSearchRequest.getPageNum() == null ? 1 : productSearchRequest.getPageNum();
        Integer pageSize = productSearchRequest.getPageSize() == null ? 20 : productSearchRequest.getPageSize();
        Integer total = list.size();
        Integer start = (pageNum - 1) * pageSize;
        Integer end = pageNum * pageSize - 1;
        if (end > total){
            // [10,19)
            list = list.subList(start, total);
        }else {
            list = list.subList(start, end + 1);
        }
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(total);
        return rspData;
    }



    /**
     * 前台商品筛选条件
     * @return
     */
    @Anonymous
    @GetMapping("/searchCondition")
    public AjaxResult productSearchCondition(){
        ProductSearchConditionVO productSearchConditionVO = productService.productSearchCondition();
        return success(productSearchConditionVO);
    }


    /**
     * 商品发布
     * @param productReleaseRequest
     * @return
     */
    @RepeatSubmit(interval = 5000)
    @PostMapping("/release")
    public AjaxResult productRelease(@RequestBody ProductReleaseRequest productReleaseRequest){
        return toAjax(productService.productRelease(productReleaseRequest));
    }
}
